<?php
    $url = new Url();
    $type_cats = LoadConfig::$type_cat; 
    $arr_status = LoadConfig::$status;
    $forder_upload = "app";  
    $link_avatar = Yii::app()->params["urlImages"]."upload_avatar_game.php?forder_upload=".$forder_upload;
    $link_url = Yii::app()->params["urlImages"]."upload_base.php?forder_upload=".$forder_upload;
?>
<link rel="stylesheet" href="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/css/swfupload.css" type="text/css" />
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/swfupload.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/swfupload.queue.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/fileprogress.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/handlers.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['tiny_url_cp'];?>js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['static_url_cp'];?>js/tinymce_submit.js"></script>

<script type="text/javascript">
    window.onload = function() {
        var configUploadData = {
            flash_url:"<?php echo Yii::app()->params["urlImages"];?>lib_upload/js/swfupload.swf",
            create_date:"<?php echo $data['create_date']; ?>",
            button_image_url:"<?php echo Yii::app()->params["urlImages"];?>lib_upload/images/buttonUpload.png",
            upload_url: "<?php echo $link_avatar;?>", 
            file_types: "*.jpg;*.jpeg;*.png;*.gif", 
            file_upload_limit : 20, 
            file_queue_limit : 1, 
            debug : false
        }
        var configUploadDataOther = {
            flash_url:"<?php echo Yii::app()->params["urlImages"];?>lib_upload/js/swfupload.swf",
            create_date:"<?php echo $data['create_date']; ?>",
            button_image_url:"<?php echo Yii::app()->params["urlImages"];?>lib_upload/images/buttonUpload.png",
            upload_url: "<?php echo $link_url;?>", 
            file_types: "*.jar;*.jad;*.apk;*.ipa;*.plist", 
            file_upload_limit : 20, 
            file_queue_limit : 1, 
            debug : false
        }
        configUpload(configUploadData);
        configUploadOther(configUploadDataOther); 
    };     
    function uploadResponse(serverData){  
        try {
            $("#txtFileName").val(""); 
            var response = $.parseJSON(serverData); // eval( "(" + serverData + ")" );      
            if(response.code==404){
                alert(response.message);return false;
            }  
            var filename = response.filename;    
            var path = response.path;
            var message = response.message;
            var code = response.code;
            var extension = response.extension;
            var filesize = response.filesize;
            var type_size = $("input[name=type_size]:checked").val();

            $("#picture").val(filename);
            $("#show_pic").html('<img src="<?php echo Yii::app()->params["urlImages"].$forder_upload.'/';?>'+path +'/'+ filename +'" class="img-100" alt="pic">');

        } catch (e) {

        };
    }

    function uploadResponseOther(serverData){  
        try {
            $("#txtFileName_other").val(""); 
            var response = $.parseJSON(serverData); // eval( "(" + serverData + ")" );      
            if(response.code==404){
                alert(response.message);return false;
            }  
            var filename = response.filename;    
            var path = response.path;
            var message = response.message;
            var code = response.code;
            var extension = response.extension;
            var filesize = response.filesize;
            var type_size = $("input[name=type_size]:checked").val();
            var str_filename = ';' + filename + "-"+extension+"-"+filesize;
            $("#file_app").val(str_filename);
            $("#show_file").html('<a target="_blank"  href="<?php echo Yii::app()->params["urlImages"].$forder_upload.'/';?>'+path +'/'+ filename +'"alt="'+filename+'" >;'+filename+'</a>');
        } catch (e) {

        };
    }

    function ajaxUpdateApp(){
        var strUrl = "<?php echo $url->createUrl("app/ajaxUpdateApp") ?>";
        var id = '<?php echo $data["id"]?>';
        tinyMCE.triggerSave();
        if (document.getElementById("isIos").checked == true) {
            isIos=1;
        }else{
            isIos=0;
        }
        if (document.getElementById("isAndroid").checked== true) {
            isAndroid=1;
        }else{
            isAndroid=0;
        }
        if (document.getElementById("isJava").checked== true) {
            isJava=1;
        }else{
            isJava=0;
        }
        if (document.getElementById("isRim").checked== true) {
            isRim=1;
        }else{
            isRim=0;
        }
        if (document.getElementById("isWindowMobi").checked== true) {
            isWindowMobi=1;
        }else{
            isWindowMobi=0;
        }
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                id:id,
                title:$("#title").val(),
                type_cat:$("#type_cat").val(),
                position:$("#position").val(),
                percent_wapmaster:$("#percent_wapmaster").val(),
                percent_partner:$("#percent_partner").val(),
                picture:$("#picture").val(),
                introtext:$("#introtext").val(),
                description:$("#description").val(),
                isIos:isIos,
                isAndroid:isAndroid,
                isJava:isJava,
                isRim:isRim,
                isWindowMobi:isWindowMobi,
                status:$("#status").val(),
            },
            success: function(msg){
                if(msg == 1){
                    alert('Cập nhật thành công');
                    window.location = '<?php echo $url->createUrl("app/index")?>';
                }else{
                    $("#show_error").html(msg);
                }
            },
            beforeSend:function(){
                $("#button_save").attr("disabled","disabled");
            },
            complete:function(){
                $("#button_save").removeAttr("disabled"); 
            }            
        });
    }
    function ajaxSaveAppFile(){
        var strUrl = "<?=$url->createUrl("app/ajaxSaveAppFile") ?>";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                appId:$("#appId").val(),
                str_filename:$("#file_app").val()
            },
            success: function(msg){
                $("#table").html(msg);
            }

        });
    }
    function ajaxUpdateAudioDetail(){

        var id = $("#audio_detail_id").val();
        var story_audio_id = $("#story_audio_id").val();
        var file_audio = $("#file_audio").val();
        var file_audio_ftp = $("#file_audio_ftp").val();
        var title_chapter = $("#title_chapter").val();
        var size = $("#size").val();
        var duration = $("#duration").val();
        var upload = $("#upload").val();
        var strUrl = "<?=$url->createUrl("audio/ajaxUpdateAudioDetail") ?>";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                id:id,
                story_audio_id:story_audio_id,
                upload:upload,
                file_audio:file_audio,
                file_audio_ftp:file_audio_ftp,
                title_chapter:title_chapter,
                size:size,
                duration:duration,
            },
            success: function(msg){
                $("#table").html(msg);
            }

        });
    }

    function ajaxDeleteAppFile(id,appId){
        var strUrl = "<?=$url->createUrl("app/ajaxDeleteAppFile") ?>";
        if(confirm('Bạn có chắc muốn xóa file này không ?')){
            $.ajax({
                type: "POST",
                url: strUrl,
                data: {
                    id:id,
                    appId:appId
                },
                success: function(msg){
                    $("#table").html(msg);
                }          
            });
        }
    }
</script>

<div class="main clearfix">
    <div class="box clearfix bottom30">  

        <div class="col-570">  
            <div clas = "box">
                <ul class="form">
                    <li class="clearfix"><label><strong>Tiêu đề </strong>:</label>
                        <div class="filltext">
                            <input type="text" style="width:360px" value="<?php echo $data['title']; ?>" id="title"> 
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Danh mục </strong>:</label>
                        <div class="filltext">
                            <select style="width:203px;" id="type_cat">
                                <?php foreach($type_cats as $key=>$value){?>
                                    <option value="<?php echo $key;?>" <?php echo $data['type'] == $key ? "selected=selected" :"";  ?>><?php echo $value?></option>
                                    <?php }?>
                            </select>
                        </div>
                    </li>

                    <li class="clearfix"><label><strong>Trạng thái</strong>:</label>
                        <div class="filltext">
                            <select style="width:203px;" id="status">
                                <?php foreach($arr_status as $key=>$value){?>
                                    <option value="<?php echo $key;?>" <?php echo $data["status"]==$key ? 'selected="selected"':''; ?>><?php echo $value?></option>
                                    <?php }?>
                            </select>
                        </div>
                    </li>
                     <li class="clearfix"><label><strong>Vị trí </strong>:</label>
                        <div class="filltext">
                            <input type="text" style="width:360px" value="<?php echo $data['position']; ?>" id="position"> 
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>% WapMaster </strong>:</label>
                        <div class="filltext">
                            <input type="text" style="width:360px" value="<?php echo $data['percent_wapmaster']; ?>" id="percent_wapmaster"> 
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>% Partner </strong>:</label>
                        <div class="filltext">
                            <input type="text" style="width:360px" value="<?php echo $data['percent_partner']; ?>" id="percent_partner"> 
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Tóm tắt</strong>:</label>
                        <div class="filltext">
                            <textarea cols="5" rows="5" style="width:477px; height:120px" id="introtext"><?php echo $data['introtext']; ?></textarea>
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Chi tiết</strong>:</label>
                        <div class="filltext">
                            <textarea cols="5" rows="5" style="width:477px; height:120px" id="description"><?php echo $data['description']; ?></textarea>
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Ảnh minh họa </strong>:</label>
                        <div class="filltext">
                            <p>
                                <input type="hidden" id="txtFileName" readonly="readonly"/>
                                <input type="text" id="urlFile" style="border:1px solid #DFDFDF; width: 200px;">                            
                                <span id="spanButtonPlaceHolder"></span>
                            </p>
                            <p><i>Định dạng file: *.jpg;*.jpeg;*.png;*.gif(Dung lượng ko được quá 2 MB)</i></p>
                            <br/>
                            <div class="fieldset flash" id="fsUploadProgress">
                                <span class="legend">File upload</span>
                            </div>

                            <input type="hidden" id="picture" value="<?php echo $data['picture']; ?>"/>
                            <p id="show_pic"><img class="img-100" src="<?php echo Common::getImage($data["picture"],"app",$data["create_date"])?>" /></p>

                        </div>
                    </li>
                    <li class="clearfix"><label><strong>OS type</strong>:</label>
                        <div class="filltext">
                            <input name="isIos" type="checkbox" id="isIos" <?php echo $data['isIos'] == 1 ? "checked=checked" :"";  ?> >isIOS 
                            <input name="isAndroid" type="checkbox" id="isAndroid" <?php echo $data['isAndroid'] == 1 ? "checked=checked" :"";  ?> >isAndroid 
                            <input name="isJava" type="checkbox" id="isJava" <?php echo $data['isJava'] == 1 ? "checked=checked" :"";  ?> >isJava 
                            <input name="isRim" type="checkbox" id="isRim" <?php echo $data['isRim'] == 1 ? "checked=checked" :"";  ?> >isRim 
                            <input name="isWindowMobi" type="checkbox" id="isWindowMobi" <?php echo $data['isWindowMobi'] == 1 ? "checked=checked" :"";  ?> >isWindowMobi 
                        </div>
                    </li>
                    <li class="clearfix"><label>&nbsp;</label>
                        <div class="filltext">
                            <input id="button_save" type="button" value=" Cập nhật " class="btn-bigblue" onclick="ajaxUpdateApp();"> 
                            &nbsp;
                            <input onclick="history.go(-1)" type="button" value=" Quay lại " class="btn-bigblue">
                        </div>
                    </li>
                    <li class="clearfix"><label>&nbsp;</label>
                        <div class="filltext" style="color: red;" id="show_error"></div>
                    </li>
                </ul>
            </div>
        </div>
        <div class="col5">&nbsp;</div>
        <div class ="col-570">
            <div class="box">
                <div class="Tabs clearfix">
                    <ul class="clearfix">
                        <li><a href="javascript:void(0)" class="active"><span>&nbsp; Upload File &nbsp;</span></a></li>

                    </ul>
                </div>
                <ul class="form" id="form1">
                    <!--<div id="data_edit_upload">-->

                    <li class="clearfix"><label><strong>File app :</strong></label>
                        <div class="filltext">
                            <p>
                                <input type="hidden" id="txtFileName_other" readonly="readonly"/>
                                <input type="text" id="urlFile_other" style="border:1px solid #DFDFDF; width: 200px;">                            
                                <span id="spanButtonPlaceHolder_other"></span>
                            </p> 
                            <p><i>Định dạng file: *.jar;*.jad;*.apk;*.ipa;*.plist(Dung lượng ko được quá 50 MB)</i></p>
                            <br/>
                            <div class="fieldset flash" id="fsUploadProgress_other">
                                <span class="legend">File upload</span>
                            </div>
                            <input type="hidden" id="file_app" value=""/>
                            <input type="hidden" id="appId" value="<?php echo $data['id']; ?>"/>
                            <p id="show_file"></p>               
                        </div>
                    </li>                    
                    <li class="clearfix"><label>&nbsp;</label>
                        <div class="filltext">
                            <input  onclick="ajaxSaveAppFile();" type="button" value="Thêm mới" class="btn-bigblue"> 
                        </div>
                    </li>
                    <br/>
                </ul>
            </div>
            <div id="table">
                <div class="table clearfix">
                    <table width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                            <tr class="bg-grey">
                                <td width="5%"><strong>STT</strong></td>
                                <td width="10%"><strong>Tên File</strong></td>
                                <td width="5%"><strong>Size</strong></td>
                                <td width="10%"><strong>Hành động</strong></td>

                            </tr>
                            <?php foreach($file_name as $key1=>$value1){$key1+=1;?>
                                <tr>
                                    <td class="middle"><?php echo $key1."</br>_</br>".$value1["id"]?></td>
                                    <td><?php echo $value1["filename"];?></td>
                                    <td><?php echo  $value1["filesize"];?></td>
                                    <td>
                                        <a href="javascript:void(0)" onclick="ajaxDeleteAppFile('<?php echo $value1["id"]?>','<?php echo $value1["appId"]?>')">  Xóa  </a> |
                                    </td>
                                </tr>
                                <?php }?>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>
</div>
